package com.prop.utils;

import java.io.FileInputStream;
import java.io.IOException;

public class FileTypeChecker {
    public static void main(String[] args) throws IOException {
        String filePath = "files/test.xls"; // 文件路径

        System.out.println(checkType(filePath));


//
//        try (FileInputStream fis = new FileInputStream(filePath)) {
//            byte[] header = new byte[8];
//            int bytesRead = fis.read(header);
//
//            if (bytesRead >= 8) {
//                if (isPpt(header)) {
//                    System.out.println("This is a PowerPoint (PPT) file.");
//                } else if (isDoc(header)) {
//                    System.out.println("This is a Word (DOC) file.");
//                } else if (isXls(header)) {
//                    System.out.println("This is an Excel (XLS) file.");
//                } else if (isPptx(header)) {
//                    System.out.println("This is a PowerPoint (PPTX) file.");
//                } else if (isDocx(header)) {
//                    System.out.println("This is a Word (DOCX) file.");
//                } else if (isXlsx(header)) {
//                    System.out.println("This is an Excel (XLSX) file.");
//                } else {
//                    System.out.println("Unknown file type.");
//                }
//            } else {
//                System.out.println("File is too small to determine the type.");
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    // 检查是否为 PPT 文件
    private static boolean isPpt(byte[] header) {
        return (header[0] == (byte) 0xD0 && header[1] == (byte) 0xCF && header[2] == 0x11 && header[3] == (byte) 0xE0
                && header[4] == (byte) 0xA1 && header[5] == (byte) 0xB1 && header[6] == 0x1A && header[7] == (byte) 0xE1);
    }

    // 检查是否为 DOC 文件
    private static boolean isDoc(byte[] header) {
        return (header[0] == (byte) 0xD0 && header[1] == (byte) 0xCF && header[2] == 0x11 && header[3] == (byte) 0xE0);
    }

    // 检查是否为 XLS 文件
    private static boolean isXls(byte[] header) {
        return (header[0] == (byte) 0x09 && header[1] == (byte) 0x08 && header[2] == 0x10 && header[3] == 0x00);
    }

    // 检查是否为 PPTX 文件
    private static boolean isPptx(byte[] header) {
        return (header[0] == (byte) 0x50 && header[1] == (byte) 0x4B && header[2] == 0x03 && header[3] == 0x04
                && header[4] == 0x14 && header[5] == 0x00 && header[6] == 0x06 && header[7] == 0x00);
    }

    // 检查是否为 DOCX 文件
    private static boolean isDocx(byte[] header) {
        return (header[0] == (byte) 0x50 && header[1] == (byte) 0x4B && header[2] == 0x03 && header[3] == 0x04
                && header[4] == 0x14 && header[5] == 0x00 && header[6] == 0x06 && header[7] == 0x00);
    }

    // 检查是否为 XLSX 文件
    private static boolean isXlsx(byte[] header) {
        return (header[0] == (byte) 0x50 && header[1] == (byte) 0x4B && header[2] == 0x03 && header[3] == 0x04
                && header[4] == 0x14 && header[5] == 0x00 && header[6] == 0x06 && header[7] == 0x00);
    }
    public static String bytesToHexString(byte[]bytes) {
        StringBuilder resultHexString = new StringBuilder();
        String tempStr = "";
        for(int i=0;i<bytes.length;i++){
            //高位清零
            tempStr = Integer.toHexString(bytes[i] & 0xff);
            if(tempStr.length() == 1){
                resultHexString.append(0).append(tempStr);
            }else {
                resultHexString.append(tempStr);
            }
        }
        return resultHexString.toString();
    }
    private static String checkType(String inputFile) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(inputFile);
        //判断文件版本
        byte[] b = new byte[4];
        fileInputStream.read(b, 0, b.length);
        String versionValue = bytesToHexString(b);
        fileInputStream.close();

        return versionValue;
    }
}


